/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.flow.business.service.FlowBusinessService;
import org.springblade.flow.core.entity.BladeFlow;
import org.springblade.modules.admin.dto.parts.PlanFlowDetailSearchDTO;
import org.springblade.modules.admin.dto.parts.SparepartPricesSearchDTO;
import org.springblade.modules.admin.entity.*;
import org.springblade.modules.admin.enums.MaintainPlanContentEnum;
import org.springblade.modules.admin.enums.StatusEnum;
import org.springblade.modules.admin.enums.TaskStatusEnum;
import org.springblade.modules.admin.mapper.SparepartPricesMapper;
import org.springblade.modules.admin.service.*;
import org.springblade.modules.admin.vo.FaultMessageVO;
import org.springblade.modules.admin.vo.MalFunctionDetailVO;
import org.springblade.modules.admin.vo.SparepartPricesVO;
import org.springblade.modules.admin.vo.WorkTrackVO;
import org.springblade.modules.admin.vo.parts.*;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 服务实现类
 *
 * @author ysq
 * @since 2022-06-28
 */
@Service
public class SparepartPricesServiceImpl extends BaseServiceImpl<SparepartPricesMapper, SparepartPrices> implements ISparepartPricesService {


	@Autowired
	private MaintainPartsWorkService maintainPartsWorkService;

	@Autowired
	private IMaintainMalfunctionWorkService malfunctionWorkService;

	@Autowired
	private IMaintainPlanRelationService planRelationService;

	@Autowired
	private MaintainMalfunctionLogService malfunctionLogService;

	@Autowired
	private FlowBusinessService flowBusinessService;

	@Autowired
	private IMaintainPlanContentService maintainPlanContentService;

	@Autowired
	private IUserService userService;

	@Autowired
	private ElevatorService elevatorService;

	@Autowired
	@Lazy
	private IMaintainPlanCheckService maintainPlanCheckService;


	@Override
	public IPage<SparepartPricesVO> selectSparepartPricesPage(SparepartPricesSearchDTO sparepartPrices) {
		Query query = new Query();
		query.setSize(sparepartPrices.getSize());
		query.setCurrent(sparepartPrices.getCurrent());
		final IPage<SparepartPricesVO> page = Condition.getPage(query);
		return page.setRecords(baseMapper.selectSparepartPricesPage(page, sparepartPrices));
	}

	@Override
	public List<SparepartPricesVO> downExcel(SparepartPricesSearchDTO sparepartPrices) {
		return baseMapper.selectSparepartPricesPage(null, sparepartPrices);
	}

	@Override
	public SparepartPricesDetailVo detail(Long id) {

		SparepartPricesDetailVo sparepartPricesDetailVo = new SparepartPricesDetailVo();
		MaintainPartsWork maintainPartsWork = maintainPartsWorkService.getDetailById(id);
		sparepartPricesDetailVo.setMaintainPartsWork(maintainPartsWork);
		//独立配件非管理维修
		if (maintainPartsWork.getFromPlanId() != null) {
			MalFunctionDetailVO malfunctionWorkDetail = malfunctionWorkService.getMalfunctionWorkDetail(maintainPartsWork.getFromPlanId());
			sparepartPricesDetailVo.setMalDetailVO(malfunctionWorkDetail);
			sparepartPricesDetailVo.setMalFunctionHistories(malfunctionWorkDetail.getMalFunctionHistories());
			if (maintainPartsWork.getGroupId() != null) {
				List<SparepartPrices> sparepartPrices = baseMapper.getSparepartPricesDetail(maintainPartsWork.getFromPlanId(), maintainPartsWork.getGroupId());
				if (sparepartPrices.size() > 0) {
					sparepartPricesDetailVo.setSparepartPrices(sparepartPrices);
				}
			}
		} else {
			List<SparepartPrices> sparepartPrices = baseMapper.getSparepartPricesDetailV2(maintainPartsWork.getPlanId());
			if (sparepartPrices.size() > 0) {
				sparepartPricesDetailVo.setSparepartPrices(sparepartPrices);
			}
		}
		WorkTrackVO workTrackVO = malfunctionLogService.selectWorkTracks(maintainPartsWork.getPlanId());
		sparepartPricesDetailVo.setWorkTrackVO(workTrackVO);
		MaintainPlanContent maintainPlanContent = maintainPlanContentService.getById(maintainPartsWork.getPlanId());
		String processInstanceId = maintainPlanContent.getProcessInstanceId();
		sparepartPricesDetailVo.setProcessInstanceId(processInstanceId);
		sparepartPricesDetailVo.setIsStop(elevatorService.getById(maintainPlanContent.getElevatorId()).getIsStop());
		sparepartPricesDetailVo.setElevatorId(maintainPlanContent.getElevatorId());
		//获取审批信息
		return sparepartPricesDetailVo;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean updateBatch(List<SparepartPrices> sparepartPricesList) {
		//让时间保持一致

		final Long userId = AuthUtil.getUserId();
		final User user = userService.getById(userId);
		final Integer priceStatus = sparepartPricesList.get(0).getPriceStatus();
		final Integer warehouseStatus = sparepartPricesList.get(0).getWarehouseStatus();
		final Date date = new Date();
		final Long groupId = System.currentTimeMillis() + RandomUtil.randomInt(50);

		//调用审批
		for (SparepartPrices sparepartPrices : sparepartPricesList) {
			BladeFlow bladeFlow = new BladeFlow();
			PlanFlowDetailSearchDTO planFlowDetailSearchDTO = new PlanFlowDetailSearchDTO();
			planFlowDetailSearchDTO.setUserId(userId);
			planFlowDetailSearchDTO.setPlanId(sparepartPrices.getPlanId());
			PlanFlowDetailVo planFlowDetailVo;
			if (sparepartPrices.getPriceStatus() != null) {
				planFlowDetailVo = baseMapper.getPricesPlanFlowDetail(planFlowDetailSearchDTO);
			} else if (sparepartPrices.getWarehouseStatus() != null) {
				planFlowDetailVo = baseMapper.getPlanFlowDetail(planFlowDetailSearchDTO);
			} else {
				throw new ServiceException("未找到审批流程！");
			}

			if (planFlowDetailVo == null || planFlowDetailVo.getTaskId() == null) {
				throw new ServiceException("未找到审批流程！");
			}
			bladeFlow.setTaskId(planFlowDetailVo.getTaskId());
			bladeFlow.setProcessInstanceId(planFlowDetailVo.getProcessInstanceId());
			if (priceStatus != null) {
				if (priceStatus == StatusEnum.DISABLE.getId()) {
					bladeFlow.setComment(sparepartPrices.getApproveDesc());
				} else {
					bladeFlow.setComment(sparepartPrices.getApproveDesc());
					bladeFlow.setFlag("ok");
				}
			} else if (warehouseStatus != null) {
				if (warehouseStatus == StatusEnum.DISABLE.getId()) {
					bladeFlow.setComment(sparepartPrices.getApproveDesc());
				} else {
					bladeFlow.setComment(sparepartPrices.getApproveDesc());
					bladeFlow.setFlag("ok");
				}
			}
			flowBusinessService.completeTask(bladeFlow);
		}

		if (priceStatus != null) {
			sparepartPricesList.forEach(e -> {
				e.setGroupId(groupId);
				e.setPriceCreateTime(date);
				e.setPriceUserName(user.getRealName());
				MaintainPlanRelation relationServiceOne = planRelationService.getOne(Wrappers.<MaintainPlanRelation>lambdaQuery().eq(MaintainPlanRelation::getOwnPlanId, e.getPlanId()));
				if (relationServiceOne != null) {
					e.setParentPlanId(relationServiceOne.getFromPlanId());
				}
				MaintainMalfunctionLog log = new MaintainMalfunctionLog();
				log.setPlanId(e.getPlanId());
				log.setExecutor(e.getPriceUserName());
				log.setExecutorId(userId);
				log.setExt(sparepartPricesList.get(0).getApproveDesc());
				if (priceStatus == StatusEnum.DISABLE.getId()) {
					log.setTypeName(TaskStatusEnum.PARTS_CLOSE.getName());
					log.setPlanStatus(TaskStatusEnum.PARTS_CLOSE.getStatus());
				} else {
					log.setTypeName(TaskStatusEnum.PARTS_SHIPMENTS.getName());
					log.setPlanStatus(TaskStatusEnum.PARTS_SHIPMENTS.getStatus());
				}
				//malfunctionLogService.save(log);
			});
			List<Long> ids = sparepartPricesList.stream().map(SparepartPrices::getPlanId).collect(Collectors.toList());
			List<MaintainPartsWork> workList = maintainPartsWorkService.list(Wrappers.<MaintainPartsWork>lambdaQuery().in(MaintainPartsWork::getPlanId, ids));
			workList.forEach(e -> e.setGroupId(groupId));
			maintainPartsWorkService.updateBatchById(workList);
			this.saveBatch(sparepartPricesList);

		}
		if (warehouseStatus != null) {
			final List<Long> planIds = sparepartPricesList.stream().map(SparepartPrices::getPlanId).collect(Collectors.toList());
			final List<SparepartPrices> pricesList = this.list(Wrappers.<SparepartPrices>lambdaQuery().in(SparepartPrices::getPlanId, planIds));
			pricesList.forEach(e -> {
				e.setWarehouseUserName(user.getRealName());
				e.setWarehouseCreateTime(date);
				e.setWaseApproveDesc(sparepartPricesList.get(0).getApproveDesc());
				e.setWarehouseStatus(warehouseStatus);
				MaintainMalfunctionLog log = new MaintainMalfunctionLog();
				log.setPlanId(e.getPlanId());
				log.setExecutor(e.getPriceUserName());
				log.setExecutorId(userId);
				log.setExt(sparepartPricesList.get(0).getWaseApproveDesc());
				if (warehouseStatus == StatusEnum.DISABLE.getId()) {
					log.setTypeName(TaskStatusEnum.PARTS_CLOSE.getName());
					log.setPlanStatus(TaskStatusEnum.PARTS_CLOSE.getStatus());
				} else {
					log.setTypeName(TaskStatusEnum.PARTS_ALLOCATION.getName());
					log.setPlanStatus(TaskStatusEnum.PARTS_ALLOCATION.getStatus());
				}
				//malfunctionLogService.save(log);
			});
			this.updateBatchById(pricesList);

		}


		return true;
	}


	@Override
	public IPage<SparepartPricesListVo> list(SparepartPricesSearchDTO sparepartPrices) {
		Query query = new Query();
		query.setSize(sparepartPrices.getSize());
		query.setCurrent(sparepartPrices.getCurrent());
		final IPage<SparepartPricesListVo> page = Condition.getPage(query);
		return page.setRecords(baseMapper.list(page, sparepartPrices));
	}

	@Override
	public SparepartPricesListDetailVo getSparepartPricesDetail(Long parentPlanId, Long groupId) {

		final MaintainMalfunctionWorkDetailVo detail = malfunctionWorkService.getDetail(parentPlanId);
		SparepartPricesListDetailVo sparepartPricesListDetailVo = new SparepartPricesListDetailVo();
		sparepartPricesListDetailVo.setMalDetailVO(detail);
//		FaultMessageVO faultMessageVO = malfunctionWorkService.getMalfunctionWorkDetail(parentPlanId).getFaultMessageVO();
//		sparepartPricesListDetailVo.setFaultMessageVO(faultMessageVO);
		sparepartPricesListDetailVo.setElevatorAddress(detail.getElevatorAddress());
		sparepartPricesListDetailVo.setBuildingName(detail.getBuildingName());
		List<SparepartPrices> sparepartPrices = baseMapper.getSparepartPricesDetail(parentPlanId, groupId);
		sparepartPricesListDetailVo.setSparepartPrices(sparepartPrices);

		//审批信息
		return sparepartPricesListDetailVo;
	}

	@Override
	public List<SparepartPricesListVo> downDetailExcel(SparepartPricesSearchDTO sparepartPricesSearchDTO) {
		return baseMapper.list(null, sparepartPricesSearchDTO);
	}

	@Override
	public IPage<SparepartPricesVO> dispatchList(SparepartPricesSearchDTO sparepartPrices) {
		Query query = new Query();
		query.setSize(sparepartPrices.getSize());
		query.setCurrent(sparepartPrices.getCurrent());
		final IPage<SparepartPricesVO> page = Condition.getPage(query);
		List<SparepartPricesVO> sparepartPricesVOS = baseMapper.dispatchList(page, sparepartPrices);
		// 2024-05-31 数据新增任务类型组合名字
		sparepartPricesVOS.forEach(e -> {
			String typeName = "";
			if(!Func.isNull(e.getSourcePlanType())){
				String name = MaintainPlanContentEnum.getName(e.getSourcePlanType());
				e.setSourcePlanTypeName(name);
				if(!Func.isNull(name)){
					typeName = name;
				}
			}
			e.setPlanTypeFullName(typeName);
		});
		return page.setRecords(sparepartPricesVOS);
	}

	@Override
	public List<SparepartPricesVO> downDispatchExcel(SparepartPricesSearchDTO sparepartPrices) {
		List<SparepartPricesVO> sparepartPricesVOS = baseMapper.dispatchListV2(null, sparepartPrices);
		sparepartPricesVOS.forEach(e -> {
			String typeName = new Integer(1).equals(e.getWorkOrderType()) ? "内部整改" : "客户报障";
			if(!Func.isNull(e.getSourcePlanType())){
				String name = MaintainPlanContentEnum.getName(e.getSourcePlanType());
				e.setSourcePlanTypeName(name);
				if(!Func.isNull(name)){
					typeName += "-";
					typeName += name;
				}
			}
			e.setPlanTypeFullName(typeName);
			// 执行人
			if(!Func.isNull(e.getUserNames())){
				String[] split = e.getUserNames().split(",");
				// 使用set去重
				Set<String> set = new HashSet<>(Arrays.asList(split));
				e.setUserNames(String.join(",", set));
			}
			// 实际执行人
			List<String> signUsers = e.getSignUsers();
			if(!Func.isNull(signUsers)){
				signUsers = signUsers.stream().distinct().collect(Collectors.toList());
				e.setSignUsers(signUsers);
			}
		});

		return sparepartPricesVOS;
	}

	@Override
	public IPage<SparepartPricesVO> planPage(SparepartPricesSearchDTO sparepartPrices) {
//		//"1:审核中,2:审核通过,3:审核不通过,4:已撤回"
//		Integer[] checkStatus = {0, 1, 2, 3, 4};
//
//		List<Integer> planStatus = new ArrayList<>(16);
//		if (sparepartPrices.getCheckStatus() != null) {
//			if (sparepartPrices.getCheckStatus().equals(checkStatus[1])) {
//				planStatus.add(TaskStatusEnum.PARTS_APPROVE.getStatus());
//				planStatus.add(TaskStatusEnum.PARTS_OFFER.getStatus());
//				planStatus.add(TaskStatusEnum.PARTS_REPLY.getStatus());
//				planStatus.add(TaskStatusEnum.PARTS_PROCUREMENT.getStatus());
//				planStatus.add(TaskStatusEnum.PARTS_SHIPMENTS.getStatus());
//				planStatus.add(TaskStatusEnum.PARTS_RECEIVE.getStatus());
//			} else if (sparepartPrices.getCheckStatus().equals(checkStatus[2])) {
//				planStatus.add(TaskStatusEnum.PARTS_ALLOCATION.getStatus());
//				planStatus.add(TaskStatusEnum.PARTS_TO_START.getStatus());
//				planStatus.add(TaskStatusEnum.PARTS_CHANGE.getStatus());
//				planStatus.add(TaskStatusEnum.PARTS_FINISH.getStatus());
//			} else if (sparepartPrices.getCheckStatus().equals(checkStatus[3])) {
//				planStatus.add(TaskStatusEnum.PARTS_CLOSE.getStatus());
//			} else if (sparepartPrices.getCheckStatus().equals(checkStatus[4])) {
//				planStatus.add(TaskStatusEnum.PARTS_WITHDRAW.getStatus());
//			}
//		}
//		if (sparepartPrices.getPlanStatus() != null && sparepartPrices.getPlanStatus().size() > 0) {
//			sparepartPrices.setStatus(sparepartPrices.getPlanStatus().get(0));
//		}
//
//		if (Func.isNotEmpty(planStatus)) {
//			final List<Integer> list = planStatus.stream().distinct().collect(Collectors.toList());
//			sparepartPrices.setPlanStatus(list);
//		}

		Query query = new Query();
		query.setSize(sparepartPrices.getSize());
		query.setCurrent(sparepartPrices.getCurrent());
		IPage<SparepartPricesVO> page = Condition.getPage(query);
		List<SparepartPricesVO> sparepartPricesVOS = baseMapper.planPage(sparepartPrices.getSize() == 0 ? null : page, sparepartPrices);
		if(Func.isNotEmpty(sparepartPricesVOS)){
			List<Long> planIds = sparepartPricesVOS.stream().map(SparepartPricesVO::getPlanId).collect(Collectors.toList());
			List<MaintainPartsWork> partsList = maintainPartsWorkService.selectListByPlans(planIds);
			// 2024-06-03 处理 审核状态名字与来源、执行人、实际执行人去重
			sparepartPricesVOS.forEach(e -> {

				e.setPartsList(partsList.stream().filter(t -> t.getPlanId().equals(e.getPlanId())).collect(Collectors.toList()));

				String typeName = "";
				typeName = new Integer(1).equals(e.getWorkOrderType()) ? "内部整改" : "客户报障";
				if(!Func.isNull(e.getSourcePlanType())){
					String name = MaintainPlanContentEnum.getName(e.getSourcePlanType());
					e.setSourcePlanTypeName(name);
					if(!Func.isNull(name)){
						typeName += "-";
						typeName += name;
					}
				}
				String taskName = TaskStatusEnum.getTaskName(MaintainPlanContentEnum.MAINTAIN_PARTS.getId(), e.getStatus());
				e.setStatusName(taskName);
				e.setPlanTypeFullName(typeName);
				// 执行人、实际执行人去重
				// 执行人
				if(!Func.isNull(e.getUserNames())){
					String[] split = e.getUserNames().split(",");
					// 使用set去重
					Set<String> set = new HashSet<>(Arrays.asList(split));
					e.setUserNames(String.join(",", set));
				}
				// 实际执行人
				List<String> signUsers = e.getSignUsers();
				if(!Func.isNull(signUsers)){
					signUsers = signUsers.stream().distinct().collect(Collectors.toList());
					e.setSignUsers(signUsers);
				}
			});
		}

		return page.setRecords(sparepartPricesVOS);
	}

	@Override
	public PlanPartSummaryVo getPlanPartSummary() {
		PlanPartSummaryVo planPartSummaryVo = new PlanPartSummaryVo();
		List<Integer> statusList = baseMapper.getPlanPartSummary(MaintainPlanContentEnum.MAINTAIN_PARTS.getId());
		// 0. 任务总数
		planPartSummaryVo.setPlanCounts(statusList.size());
		// 需求审批
		final List<Integer> applyCounts = statusList.stream().filter(o -> o == TaskStatusEnum.PARTS_APPLY.getStatus()).collect(Collectors.toList());
		planPartSummaryVo.setApplyCounts(applyCounts.size());
		// 信息审核
		final List<Integer> inCheckCounts = statusList.stream().filter(o -> o == TaskStatusEnum.PARTS_APPROVE.getStatus()).collect(Collectors.toList());
		planPartSummaryVo.setInCheckCounts(inCheckCounts.size());
		// 销售生单
		final List<Integer> disPriceCounts = statusList.stream().filter(o -> o == TaskStatusEnum.PARTS_OFFER.getStatus() || o == TaskStatusEnum.PARTS_REPLY.getStatus() ).collect(Collectors.toList());
		planPartSummaryVo.setDisPriceCounts(disPriceCounts.size());
		// 待发货
		final List<Integer> disWarehouseCounts = statusList.stream().filter(o -> o == TaskStatusEnum.PARTS_SHIPMENTS.getStatus() || o == TaskStatusEnum.PARTS_PROCUREMENT.getStatus()).collect(Collectors.toList());
		planPartSummaryVo.setDisWarehouseCounts(disWarehouseCounts.size());

		// 待签收
		planPartSummaryVo.setWaitReceiveCounts(
			(int) statusList.stream().filter(o -> o == TaskStatusEnum.PARTS_RECEIVE.getStatus()).count()
		);
		// 待分配
		final List<Integer> disPlanUserCounts = statusList.stream().filter(o -> o == TaskStatusEnum.PARTS_ALLOCATION.getStatus()).collect(Collectors.toList());
		planPartSummaryVo.setDisPlanUserCounts(disPlanUserCounts.size());

		// 待更换
		final List<Integer> disSignCounts = statusList.stream().filter(o -> o == TaskStatusEnum.PARTS_TO_START.getStatus()).collect(Collectors.toList());
		planPartSummaryVo.setDisSignCounts(disSignCounts.size());

		// 更换中
		final List<Integer> enSignCounts = statusList.stream().filter(o -> o == TaskStatusEnum.PARTS_CHANGE.getStatus()).collect(Collectors.toList());
		planPartSummaryVo.setEnSignCounts(enSignCounts.size());

		// 已完成
		final List<Integer> finishCounts = statusList.stream().filter(o -> o == TaskStatusEnum.PARTS_FINISH.getStatus()).collect(Collectors.toList());
		planPartSummaryVo.setFinishCounts(finishCounts.size());

		// 已终止
		final List<Integer> disCheckCounts = statusList.stream().filter(o -> o == TaskStatusEnum.PARTS_CLOSE.getStatus()).collect(Collectors.toList());
		planPartSummaryVo.setDisCheckCounts(disCheckCounts.size());

		// 已退回，单独查询
		int rejectCount = maintainPartsWorkService.rejectCount();
		planPartSummaryVo.setRejectCounts(rejectCount);

		// 已撤回
		final List<Integer> revocationCounts = statusList.stream().filter(o -> o == TaskStatusEnum.PARTS_WITHDRAW.getStatus()).collect(Collectors.toList());
		planPartSummaryVo.setRevocationCounts(revocationCounts.size());

		return planPartSummaryVo;
	}

	@Override
	public List<PartIconVo> getPartIconList() {
		//1:配件报价,2:配件发货3:配件调度4:质检任务
//		Query query = new Query();
//		query.setSize(10);
//		query.setCurrent(1);
//		IPage<SparepartPricesVO> page = Condition.getPage(query);
		SparepartPricesSearchDTO sparepartPrices = new SparepartPricesSearchDTO();
//		//待报价
//		int priceStatus = 2;
//		sparepartPrices.setSearchType(1);//表示报价列表
//		sparepartPrices.setPriceStatus(priceStatus);
//		page.setRecords(baseMapper.selectSparepartPricesPage(page, sparepartPrices));
//
//
		List<PartIconVo> list = new ArrayList<>(16);
		Integer[] types = {0, 1, 2, 3, 4};
//		PartIconVo partIconVo1 = new PartIconVo();
//		partIconVo1.setType(types[1]);
//		partIconVo1.setTypeName("配件报价");
//		partIconVo1.setCounts(Convert.toInt(page.getTotal()));
//
//
//		//待发货
//		SparepartPricesSearchDTO sparepartPrices2 = new SparepartPricesSearchDTO();
//		int warehouseStatus = 2;
//		sparepartPrices2.setSearchType(2);//表示报价列表
//		priceStatus = 1;//已报价
//		sparepartPrices2.setPriceStatus(priceStatus);
//		sparepartPrices2.setWarehouseStatus(warehouseStatus);
//		sparepartPrices2.setPriceStatus(1);
//		Query query2 = new Query();
//		query2.setSize(10);
//		query2.setCurrent(1);
//		IPage<SparepartPricesVO> page2 = Condition.getPage(query2);
//		page2.setRecords(baseMapper.selectSparepartPricesPage(page2, sparepartPrices2));
//		PartIconVo partIconVo2 = new PartIconVo();
//		partIconVo2.setType(types[2]);
//		partIconVo2.setTypeName("配件发货");
//		partIconVo2.setCounts(Convert.toInt(page2.getTotal()));


		SparepartPricesSearchDTO sparepartPrices3 = new SparepartPricesSearchDTO();
		Query query3 = new Query();
		query3.setSize(sparepartPrices.getSize());
		query3.setCurrent(sparepartPrices.getCurrent());
		int status = 4;//待分配
		sparepartPrices3.setStatus(status);
		IPage<SparepartPricesVO> page3 = Condition.getPage(query3);
		page3.setRecords(baseMapper.dispatchListV2(page3, sparepartPrices3));


		PartIconVo partIconVo3 = new PartIconVo();
		partIconVo3.setType(types[3]);
		partIconVo3.setTypeName("配件调度");
		partIconVo3.setCounts(Convert.toInt(page3.getTotal()));

		list.add(partIconVo3);
		return list;
	}

	@Override
	public List<PartIconVo> getPartIconListV2() {
		return null;
	}

	/**
	 * 2024-05-06 维修任务详情配件任务新接口方法
	 * @param parentId 父id
	 * @return List<SparepartPricesVO> 关联配件任务列表
	 */
	@Override
	public List<SparepartPricesVO> getRelationPartsByParentId(Long parentId) {
		List<SparepartPricesVO> result = baseMapper.getRelationPartsByParentId(parentId);
		if(!Func.isEmpty(result)){
			result.forEach(e -> {
				if(!Func.isNull(e.getPartsId())){
					List<Long> partIds = new ArrayList<>();
					String[] split = e.getPartsId().split(",");
					for (String s : split) {
						partIds.add(Long.parseLong(s));
					}
					e.setPartIds(partIds);
				}
			});
		}
		return result;
	}

	@Override
	public IPage<SparepartPricesVO> dispatchListV2(SparepartPricesSearchDTO sparepartPrices) {
		Query query = new Query();
		query.setSize(sparepartPrices.getSize());
		query.setCurrent(sparepartPrices.getCurrent());
		final IPage<SparepartPricesVO> page = Condition.getPage(query);
		List<SparepartPricesVO> sparepartPricesVOS = baseMapper.dispatchListV2(page, sparepartPrices);
		// 2024-05-31 数据新增任务类型组合名字
		if(Func.isNotEmpty(sparepartPricesVOS)){
			List<MaintainPartsWork> partsList = maintainPartsWorkService.selectListByPlans(sparepartPricesVOS.stream().map(SparepartPricesVO::getPlanId).collect(Collectors.toList()));
			sparepartPricesVOS.forEach(e -> {
				e.setPartsList(partsList.stream().filter(t -> t.getPlanId().equals(e.getPlanId())).collect(Collectors.toList()));
				String typeName = new Integer(1).equals(e.getWorkOrderType()) ? "内部整改" : "客户报障";
				if(!Func.isNull(e.getSourcePlanType())){
					String name = MaintainPlanContentEnum.getName(e.getSourcePlanType());
					e.setSourcePlanTypeName(name);
					if(!Func.isNull(name)){
						typeName += "-";
						typeName += name;
					}
				}
				e.setPlanTypeFullName(typeName);
				// 执行人
				if(!Func.isNull(e.getUserNames())){
					String[] split = e.getUserNames().split(",");
					// 使用set去重
					Set<String> set = new HashSet<>(Arrays.asList(split));
					e.setUserNames(String.join(",", set));
				}
				// 实际执行人
				List<String> signUsers = e.getSignUsers();
				if(!Func.isNull(signUsers)){
					signUsers = signUsers.stream().distinct().collect(Collectors.toList());
					e.setSignUsers(signUsers);
				}
			});
		}
		return page.setRecords(sparepartPricesVOS);
	}

}
